// 所有JIRA 项目都是固定的，所有他们的key或id也是固定的。直接保存数组维护即可，无需动态获取.

// 从rest api中获取当前项目版本号列表
// 接口形式: http://jira.in.zhihu.com/rest/api/2/project/{Project key or Id}/versions
// 如: 
// http://jira.in.zhihu.com/rest/api/2/project/QA/versions
// http://jira.in.zhihu.com/rest/api/2/project/10119/versions 获取

// var request = require('request');
// // var qs = require('querystring');
// request.get('http://jira.in.zhihu.com/rest/api/2/project/10119/versions', function(e, r, user) {
//     console.log(user);
// });


const Client = require('node-rest-client').Client;
// auth
const Options_auth = { user: "chendong", password: "Chendong@2018" };
var client = new Client(Options_auth);


// client.get('http://jira.in.zhihu.com/rest/api/2/project/QA/versions', function(data, response) {
//     console.log(data);
//     // console.log(response.statusCode); // 200 -> success
// });

function getID(url) {
    return new Promise(function(resolve, reject) {
        client.get(url, function(data, response) {
            if (response.statusCode == '200') {
                resolve(data);
            } else {
                reject(response.statusCode);
            }
        })
    });
}

// 只保留版本号列表的id/projectId/描述/name 这四项
function updateData(arr) {

}


var doAction = async function(url) {
    var arr = await getID(url);
    // 此处拿到版本号列表数据后，对数据进行操作
    // 打印数据
    // console.log(a);
    // 调研其它函数执行处理数据 ... ... 等等
    // 去除无用信息
    var res_new = arr.map(function(info) {
        delete info.archived;
        delete info.released;
        delete info.description;
        delete info.self;
        return info;
    });

    console.log(res_new);
    // 或在此 执行其它方法: 如HTTP请求数据提交等操作
    //  ....

}

// 执行action
doAction('http://jira.in.zhihu.com/rest/api/2/project/QA/versions');